package com.canbot.u05.activity.customvoice.tool;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.canbot.u05.R;


/**
 * Created by liu.heng on 2018/5/24.
 */

public class CommomDialog extends Dialog implements View.OnClickListener {

        private TextView contentTxt;

        private TextView titleTxt;

        private TextView submitTxt;

        private TextView cancelTxt;

        private ImageView mImageView;

        private Context mContext;

        private String content;

        private OnCloseListener listener;

        private String positiveName;

        private String negativeName;

        private String title;

        private Boolean isNoTitle = false;

        private Boolean isOnlyOne = false;

        private Boolean isHaveImage = false;

        public CommomDialog(Context context) {
                super(context);
                this.mContext = context;
        }

        public CommomDialog(Context context, int themeResId, String content) {
                super(context, themeResId);
                this.mContext = context;
                this.content = content;
        }

        public CommomDialog(Context context, int themeResId, String content, OnCloseListener listener) {
                super(context, themeResId);
                this.mContext = context;
                this.content = content;
                this.listener = listener;
        }

        //底部只有有一个按钮
        public CommomDialog(Context context, int themeResId, String content, Boolean isOnlyOne, String positiveName,
                            OnCloseListener listener) {
                super(context, themeResId);
                this.mContext = context;
                this.content = content;
                this.listener = listener;
                this.isOnlyOne = isOnlyOne;
                this.positiveName = positiveName;
        }

        //底部只有有一个按钮,且内容包含图片
        public CommomDialog(Context context, int themeResId, String content, Boolean isOnlyOne,
                            Boolean isHaveImage, Boolean isNoTitle, String positiveName,
                            OnCloseListener listener) {
                super(context, themeResId);
                this.mContext = context;
                this.content = content;
                this.listener = listener;
                this.isOnlyOne = isOnlyOne;
                this.isHaveImage = isHaveImage;
                this.isNoTitle = isNoTitle;
        }

        protected CommomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
                super(context, cancelable, cancelListener);
                this.mContext = context;
        }

        public CommomDialog setTitle(String title) {
                this.title = title;
                return this;
        }

        public CommomDialog setPositiveButton(String name) {
                this.positiveName = name;
                return this;
        }

        public CommomDialog setNegativeButton(String name) {
                this.negativeName = name;
                return this;
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.dialog_common);
                setCanceledOnTouchOutside(false);
                initView();
        }

        private void initView() {
                contentTxt = (TextView) findViewById(R.id.content);
                titleTxt = (TextView) findViewById(R.id.title);
                submitTxt = (TextView) findViewById(R.id.submit);
                submitTxt.setOnClickListener(this);
                cancelTxt = (TextView) findViewById(R.id.cancel);
                cancelTxt.setOnClickListener(this);
                mImageView = (ImageView) findViewById(R.id.my_image);

                contentTxt.setText(content);
                if (!TextUtils.isEmpty(positiveName)) {
                        submitTxt.setText(positiveName);
                }

                if (!TextUtils.isEmpty(negativeName)) {
                        cancelTxt.setText(negativeName);
                }

                if (!TextUtils.isEmpty(title)) {
                        titleTxt.setText(title);
                }

                if (isNoTitle) {
                        titleTxt.setText("");
                }
                if (isOnlyOne) {
                        cancelTxt.setVisibility(View.GONE);
                }
                if (isHaveImage) {
                        mImageView.setVisibility(View.VISIBLE);
                }
        }

        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                        case R.id.cancel:
                                if (listener != null) {
                                        listener.onClick(this, false);
                                }
                                this.dismiss();
                                break;
                        case R.id.submit:
                                if (listener != null) {
                                        listener.onClick(this, true);
                                }
                                break;
                        default:
                                break;
                }
        }

        public interface OnCloseListener {

                void onClick(Dialog dialog, boolean confirm);
        }
}
